package com.xingkeduo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.model.dto.*;
import com.xingkeduo.model.vo.OrderInfoVo;
import com.xingkeduo.model.vo.ShopInfoVo;
import com.xingkeduo.model.vo.ShopVo;
import com.xingkeduo.model.vo.StaffInfoVo;
import com.xingkeduo.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2019/5/10 17:21
 * @Created 情绪十分不稳定的Shadow
 */

@Service
public class OtherServiceImpl implements OtherService {

    private static Logger log = LoggerFactory.getLogger(OtherServiceImpl.class);

    //用户服务
    @Autowired
    BusinessuserFeignService businessuserFeignService;

    //大后台服务
    @Autowired
    OperationService operationService;

    //订单服务
    @Autowired
    OrderService orderService;

    @Autowired
    MarketingFeignService marketingFeignService;

    @Override
    public List<StaffInfoVo> getStaffInfoVo(List<Long> staffIdList) {
        //从businessuser服务获取对应店铺下的发型师头像and艺名
        String result = businessuserFeignService.getshop(staffIdList, "1");
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        System.out.println(data);
        JSONArray tableData = JSONArray.parseArray(data);
        List<StaffInfoVo> staffInfoVoList = JSONObject.parseArray(tableData.toJSONString(), StaffInfoVo.class);
        return staffInfoVoList;

    }

    @Override
    public List<ShopIdAndStaffInfoDto> getStaffTimingTaskDto(List<Long> shopIds) {
        log.info("定时排班任务getStaffTimingTaskDto入参shopIds--->>>"+shopIds);
        String result = businessuserFeignService.getStaffInfoByShopIds(shopIds);
        log.info("定时排班任务getStaffTimingTaskDto结果result--->>>"+result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        JSONArray tableData = JSONArray.parseArray(data);
        List<ShopIdAndStaffInfoDto> shopIdAndStaffInfoDtos = JSONObject.parseArray(tableData.toJSONString(), ShopIdAndStaffInfoDto.class);
        return shopIdAndStaffInfoDtos;
    }

    @Override
    public ShopAndStaffListDto getCraftsmanServiceList(String shopId, List<Long> craftsmanIds) {
        //准备请求数据
        JSONObject object = new JSONObject();
        object.put("shopId",shopId);
        String result = businessuserFeignService.getCraftsmanServiceList(object.toString(), craftsmanIds);
        System.out.println("发型师Sku&价格："+result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        // TODO 判断返回结果中是否存在数据 暂时返回null
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }
        return JSON.parseObject(data, ShopAndStaffListDto.class);
    }

    @Override
    public List<StaffIdAndServiceIdDto> getStaffIdAndServiceIds(List<Long> staffIdList) {
        String result = businessuserFeignService.getServiceids(staffIdList);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        JSONArray tableData = JSONArray.parseArray(data);
        List<StaffIdAndServiceIdDto> staffIdAndServiceIdDtos = JSONObject.parseArray(tableData.toJSONString(), StaffIdAndServiceIdDto.class);
        return staffIdAndServiceIdDtos;
    }


    @Override
    public ShopInfoVo getShopInfoByShopId(Long storeId) {
        //准备请求数据
        JSONObject object = new JSONObject();
        object.put("id",storeId.toString());
        String result = operationService.grain(object.toString());
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        // TODO 判断返回结果中是否存在数据 暂时返回null
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }

        return JSON.parseObject(data, ShopInfoVo.class);
    }

    @Override
    public List<OrderInfoVo> getOrderInfoByOrderNos(String[] orderNos) {
        String result = orderService.queryByOrderNos(orderNos);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }
        JSONArray tableData = JSONArray.parseArray(data);
        List<OrderInfoVo> orderInfoVoList = JSONObject.parseArray(tableData.toJSONString(), OrderInfoVo.class);
        for(OrderInfoVo orderInfoVo : orderInfoVoList) {
            if (StringUtils.isEmpty(orderInfoVo.getAppointmentStartTime()))
                orderInfoVo.setAppointmentStartTime("");
            if (StringUtils.isEmpty(orderInfoVo.getUserMobile()))
                orderInfoVo.setUserMobile("");
            if (StringUtils.isEmpty(orderInfoVo.getWaitingLineNo()))
                orderInfoVo.setWaitingLineNo("");
        }
        return orderInfoVoList;
    }



    @Override
    public List<ShopVo> getShopByStatus(Long shopStatus) {
        //准备请求数据
        log.info("定时排班任务getShopByStatus，接口请求店铺绑定人员信息，入参shopStatus--->>>"+shopStatus);
        String result = operationService.getShopsList(shopStatus);
        log.info("定时排班任务getShopByStatus，接口请求结果result--->>>"+result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        // TODO 判断返回结果中是否存在数据 暂时返回null
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }
        JSONArray tableData = JSONArray.parseArray(data);
        List<ShopVo> shopVos = JSONObject.parseArray(tableData.toJSONString(), ShopVo.class);
        return shopVos;
    }

    @Override
    public List<ShopSkuAppInfoDto> getStoreSkuInfoListByStoreId(Long storeId) {
        //准备请求数据
        String result = operationService.getStoreSkuInfoListByStoreId(storeId);
        log.info("最小sku："+result);
        System.out.println("最小sku："+result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        // TODO 判断返回结果中是否存在数据 暂时返回null
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }
        JSONArray tableData = JSONArray.parseArray(data);
        List<ShopSkuAppInfoDto> shopSkuAppInfoDtos = JSONObject.parseArray(tableData.toJSONString(), ShopSkuAppInfoDto.class);
        return shopSkuAppInfoDtos;
    }


    @Override
    public List<ShopSkuInfoDto> getShopsServprodList(Long shopId) {
        //准备请求数据
        log.info("准备请求参数shopId"+shopId);
        String result = operationService.getShopsServprodList(shopId);
        log.info("获取当前店铺下sku&价格"+result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        // TODO 判断返回结果中是否存在数据 暂时返回null
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }
        JSONArray tableData = JSONArray.parseArray(data);
        List<ShopSkuInfoDto> shopVos = JSONObject.parseArray(tableData.toJSONString(), ShopSkuInfoDto.class);
        return shopVos;
    }

    @Override
    public ScreenPriceDto getScreenPriceInfo(Long shopId) {
        //准备请求数据
        String result = operationService.getScreenPriceInfo(shopId);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        // TODO 判断返回结果中是否存在数据 暂时返回null
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }

        return JSON.parseObject(data, ScreenPriceDto.class);
    }

    @Override
    public Map<String, String> getHairdresserActivitySummaryInfo(Long storeId, List<Long> staffIds) {
        //准备请求数据
        String result = marketingFeignService.getHairdresserActivitySummaryInfo(storeId, staffIds);
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果 TODO status=0 暂时返回null
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        System.out.println(data);
        // TODO 判断返回结果中是否存在数据 暂时返回null
        if(org.springframework.util.StringUtils.isEmpty(data)){
            return null;
        }
        Map<String, String> mapObj = JSONObject.parseObject(data,Map.class);
        return mapObj;
    }

    /**
     * 根据二线用户ID列表，获取二线用户信息
     * @param ids
     * @return
     */
    @Override
    public List<ManagerDTO> getManagerList(List<Long> ids) {

        String managerList = operationService.getManagerList(ids);
        JSONObject jsonObject = JSONObject.parseObject(managerList);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        System.out.println(data);
        JSONArray tableData = JSONArray.parseArray(data);
        List<ManagerDTO> staffInfoVoList = JSONObject.parseArray(tableData.toJSONString(), ManagerDTO.class);
        return staffInfoVoList;

    }

    /**
     * cms获取店铺详情
     * @param storeId
     * @return
     */
    @Override
    public CmsShopsInfoDTO getShopInfo(Long storeId) {
        JSONObject object = new JSONObject();
        object.put("id",storeId);
        String shopInfo = operationService.getShopInfo(object.toString());
        JSONObject jsonObject = JSONObject.parseObject(shopInfo);
        String status = jsonObject.get("status").toString();
        //判断返回结果
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        return JSON.parseObject(data, CmsShopsInfoDTO.class);
    }

//CMS获取店铺活动信息
    @Override
    public List<CmsActivityResponseDTO> getShopActivityList(Long storeId) {

        String managerList = marketingFeignService.getShopActivityList(storeId);
        JSONObject jsonObject = JSONObject.parseObject(managerList);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        System.out.println(data);
        JSONArray tableData = JSONArray.parseArray(data);
        List<CmsActivityResponseDTO> cmsActivityResponseDTOS = JSONObject.parseArray(tableData.toJSONString(), CmsActivityResponseDTO.class);
        return cmsActivityResponseDTOS;

    }



    //根据店铺ID和分公司ID获取店铺经理和总经理信息
    @Override
    public CmsCraftsmanShopDTO getManagerList(Long shopId, Long companyId) {

        String managerList = businessuserFeignService.getManagerList(companyId,shopId);
        JSONObject jsonObject = JSONObject.parseObject(managerList);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果
        if("0".equals(status)){
            return null;
        }
        String data = jsonObject.get("data").toString();
        return JSON.parseObject(data, CmsCraftsmanShopDTO.class);

    }


    @Override
    public List<Long> getCmsCraftsmanList(String condition) {

        JSONObject object = new JSONObject();
        object.put("condition",condition);
        String craftsmanNameList = businessuserFeignService.getCraftsmanNameList(URLEncoder.encode(object.toJSONString()));
        JSONObject jsonObject = JSONObject.parseObject(craftsmanNameList);
        String status = jsonObject.get("status").toString();
        //判断服务返回结果
        if("0".equals(status)){
            return null;
        }
        List<Long> userList = new ArrayList<>();
        JSONArray array = jsonObject.getObject("data", JSONArray.class);
        if (array != null && array.size()>0) {
           userList = array.toJavaList(Long.class);
        }
        return userList;

    }

}
